package com.sunny.service;

import com.sunny.dataobject.ProductInfo;
import com.sunny.dto.CartDTO;
import com.sunny.enums.ProductStatusEnum;
import com.sunny.enums.ResultEnum;
import com.sunny.exception.SellException;
import com.sunny.repository.ProductInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

/**
 * Created by dell on 2018/3/29.
 */
@Service
public class ProductServiceImpl  implements ProductService {
    @Autowired
    private ProductInfoRepository repository;

    public ProductInfo findOne(String productId) {
        ProductInfo productInfo=new ProductInfo();
        productInfo.setProductId(productId);
        Example<ProductInfo> example=Example.of(productInfo);
        Optional<ProductInfo> result = repository.findOne(example);
        return result.get();
    }

    public List<ProductInfo> findUpAll() {
        return repository.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    public Page<ProductInfo> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }

    public ProductInfo save(ProductInfo productInfo) {
        return repository.save(productInfo);
    }

    /**
     * 增加库存
     * @param cartDTOList
     */
    @Transactional
    public void increaseStock(List<CartDTO> cartDTOList) {
        for(CartDTO cartDTO:cartDTOList){
            ProductInfo productInfo=new ProductInfo();
            productInfo.setProductId(cartDTO.getProductId());
            Example<ProductInfo> example=Example.of(productInfo);
            ProductInfo productInfo1 = repository.findOne(example).get();
            if(productInfo1==null){
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer result=productInfo1.getProductStock()+cartDTO.getProductQuantity();
            productInfo1.setProductStock(result);
            repository.save(productInfo1);
        }

    }
    //加个事物，要么成功or失败,防止并发
    @Transactional
    public void decreaseStock(List<CartDTO> cartDTOList) {
           for(CartDTO cartDTO:cartDTOList){
               ProductInfo pro=new ProductInfo();
               pro.setProductId(cartDTO.getProductId());
               Example<ProductInfo> example=Example.of(pro);
               Optional<ProductInfo> res = repository.findOne(example);
               ProductInfo productInfo=res.get();
               if(productInfo==null){
                   throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
               }
               Integer result = productInfo.getProductStock() - cartDTO.getProductQuantity();
               if(result<0){
                   throw  new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
               }
               productInfo.setProductStock(result);
               repository.save(productInfo);
           }
    }
}
